/*
 *  Copyright 2008-2010 the original author or authors.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.openaecfm.spf.io.geometry;

import java.util.ArrayList;
import java.util.List;

import org.openaecfm.model.geometry.BSplineSurface;
import org.openaecfm.model.geometry.BSplineSurfaceForm;
import org.openaecfm.model.geometry.CartesianPoint;
import org.openaecfm.model.measure.Logical;
import org.openaecfm.spf.io.IAppDataManager;
import org.openaecfm.spf.io.IEntityInstanceUtilInput;
import org.openaecfm.spf.io.IEntityInstanceUtilOutput;
import org.openaecfm.spf.model.EntityInstance;
import org.openaecfm.spf.model.EntityInstanceNameParameter;
import org.openaecfm.spf.model.EnumerationParameter;
import org.openaecfm.spf.model.IntegerParameter;
import org.openaecfm.spf.model.ListParameter;
import org.openaecfm.spf.model.Parameter;
import org.openaecfm.spf.model.SimpleEntityInstance;

public final class BSplineSurfaceIO implements
		IEntityInstanceUtilOutput<BSplineSurface>,
		IEntityInstanceUtilInput<BSplineSurface> {

	@Override
	public void resolveNotReferenceParameter(EntityInstance ei,
			BSplineSurface pojo) {
		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set UDegree
		IntegerParameter u = (IntegerParameter) sei.getParameter(0);
		pojo.setUDegree(u.toInteger());

		// Set VDegree
		IntegerParameter v = (IntegerParameter) sei.getParameter(1);
		pojo.setVDegree(v.toInteger());

		// Set SurfaceForm
		EnumerationParameter surfaceForm = (EnumerationParameter) sei
				.getParameter(3);
		pojo.setSurfaceForm(BSplineSurfaceForm.valueOf(surfaceForm
				.toEnumString()));

		// Set UClosed
		EnumerationParameter uClosed = (EnumerationParameter) sei
				.getParameter(4);
		pojo.setUClosed(Logical.valueOf(uClosed.toEnumString()));

		// Set VClosed
		EnumerationParameter vClosed = (EnumerationParameter) sei
				.getParameter(5);
		pojo.setVClosed(Logical.valueOf(vClosed.toEnumString()));

		// Set SelfIntersect
		EnumerationParameter selfIntersect = (EnumerationParameter) sei
				.getParameter(6);
		pojo.setSelfIntersect(Logical.valueOf(selfIntersect.toEnumString()));
	}

	@Override
	public void resolveReferenceParameter(EntityInstance ei,
			BSplineSurface pojo, IAppDataManager appDataManager) {
		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set ControlPointsList
		ListParameter cList = (ListParameter) sei.getParameter(2);
		for (Parameter p : cList) {
			ListParameter l = (ListParameter) p;
			List<CartesianPoint> lc = new ArrayList<CartesianPoint>();
			for (Parameter pa : l) {
				EntityInstanceNameParameter e = (EntityInstanceNameParameter) pa;
				lc.add((CartesianPoint) appDataManager.getObject(e
						.toInstanceName()));
			}
			pojo.getControlPointsList().add(lc);
		}
	}

	@Override
	public void exportSPF(BSplineSurface pojo, EntityInstance ei,
			IAppDataManager appDataManager) {
		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set UDegree
		sei.addParameter(new IntegerParameter(pojo.getUDegree()));

		// Set VDegree
		sei.addParameter(new IntegerParameter(pojo.getVDegree()));

		// Set ControlPointsList
		ListParameter cList = new ListParameter();
		for (List<CartesianPoint> lc : pojo.getControlPointsList()) {
			ListParameter l = new ListParameter();
			for (CartesianPoint point : lc) {
				l.add(new EntityInstanceNameParameter(appDataManager
						.getName(point)));
			}
			cList.add(l);
		}
		sei.addParameter(cList);

		// Set SurfaceForm
		sei.addParameter(new EnumerationParameter(pojo.getSurfaceForm()));

		// Set UClosed
		sei.addParameter(new EnumerationParameter(pojo.getUClosed()));

		// Set VClosed
		sei.addParameter(new EnumerationParameter(pojo.getVClosed()));

		// Set SelfIntersect
		sei.addParameter(new EnumerationParameter(pojo.getSelfIntersect()));
	}
}
